//
//  OxPopMenuView.m
//  系统学习每一个控件
//
//  Created by 陈强 on 16/3/16.
//  Copyright © 2016年 com.uj345. All rights reserved.
//

#import "OxPopMenuView.h"
#import "XMGVerticalButton.h"
#import <POP.h>

#define kW [UIScreen mainScreen].bounds.size.width
#define kH [UIScreen mainScreen].bounds.size.height
#define iconTextColor [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]

@interface OxPopMenuView(){
    UIView *_blurV;
    UIView *_contentV;
    UIButton *_cancleBt;
    OxPopMenuViewButtonClickBlock _block;
}
@end

@implementation OxPopMenuView

#pragma mark - 显示方法
- (void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
}

# pragma mark - 构建方法 
- (instancetype)initWithItems:(NSArray *)items {
    self = [super init];
    if(self) {
        self.frame = (CGRect){{0,0},kW,kH};
        self.backgroundColor = [UIColor clearColor];
        [self initUIWithItems:items];
    }
    return self;
}

- (void)initUIWithItems:(NSArray *)items {
    _blurV = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _blurV.layer.contents = (id)ImageWithName(@"分享背景").CGImage;
    _blurV.alpha = 0.8;
    _blurV.userInteractionEnabled = NO;
    [self addSubview:_blurV];
    
    [self getContentViewWithItems:items];
    
    for (int i = 0; i<items.count; i++) {//每个按钮的宽度是(kW - 5*15)/4 高度是宽度+20 tag 100+
        CGFloat margin = 15;
        int h = i/4 ;//行
        int l = i%4 ;//列
        CGFloat width = (kW - 75)/4.0;
        CGFloat height = width + 20;
        CGFloat x = margin + (margin + width)*l;
        CGFloat endY  = CGRectGetMinY(_contentV.frame) + 15 + (height + 15)*h;
        CGFloat beginY = endY - kH;
        
        XMGVerticalButton *button = [[XMGVerticalButton alloc]init];
        button.tag = 100 + i;
        button.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [button setTitleColor:iconTextColor forState:UIControlStateNormal];
        [button addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:items[i]] forState:UIControlStateNormal];
        [button setTitle:items[i] forState:UIControlStateNormal];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:button];
        
        // 按钮动画
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(x,beginY, width, height)];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(x,endY, width, height)];
        anim.springBounciness = 10.f;
        anim.springSpeed = 10.f;
        anim.beginTime = CACurrentMediaTime() + 0.1 * i;
        [button pop_addAnimation:anim forKey:nil];
    }
}

/** 创建内容视图 */
- (void)getContentViewWithItems:(NSArray *)items {
    CGFloat width = kW;
    CGFloat height = 15 + ((items.count-1)/4+1)*((kW - 75)/4 + 20) + 15*(items.count/4) + 15 + 50 + 20;//top + 按钮的总高度 + 按钮间距 + 按钮和叉叉的间距 + 叉叉高度 + bottom
    _contentV = [[UIView alloc]initWithFrame:(CGRect){{0,kH-height},width,height}];
    _contentV.clipsToBounds = YES;
    _contentV.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];
    [self addSubview:_contentV];
    
    _cancleBt = [UIButton buttonWithType:UIButtonTypeCustom];
    [_cancleBt setImage:[UIImage imageNamed:@"取消分享"] forState:UIControlStateNormal];
    [_cancleBt addTarget:self action:@selector(cancleBtClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancleBt];
    // 按钮动画
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
    anim.fromValue = [NSValue valueWithCGRect:(CGRect){{(kW - 50)/2.0,CGRectGetMinY(_contentV.frame) + (height - 20 - 50) - kH},50,50}];
    anim.toValue = [NSValue valueWithCGRect:(CGRect){{(kW - 50)/2.0,CGRectGetMinY(_contentV.frame) + (height - 20 - 50)},50,50}];
    anim.springBounciness = 10.f;
    anim.springSpeed = 10.f;
    anim.beginTime = CACurrentMediaTime() + (items.count+1)*0.15;
    [_cancleBt pop_addAnimation:anim forKey:nil];
    [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        // 标语动画执行完毕, 恢复点击事件
        _blurV.userInteractionEnabled = YES;
    }];
}

#pragma mark - 点击事件
- (void)buttonClick:(OxPopMenuViewButtonClickBlock)block {
    _block = block;
}

- (void)shareButtonClick:(UIButton *)button {
    if (_block) {
        _block(button);
    }
    NSLog(@"点击分享按钮");
}

- (void)cancleBtClick:(UIButton *)button {
    
    _blurV.userInteractionEnabled = NO;
    int beginIndex = 2;
    
    for (int i = beginIndex; i<self.subviews.count; i++) {
        UIView *subview = self.subviews[i];
        // 基本动画
        POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
        CGFloat centerY = subview.centerY + kH;
        // 动画的执行节奏(一开始很慢, 后面很快)
        //        anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        anim.toValue = [NSValue valueWithCGPoint:CGPointMake(subview.centerX, centerY)];
        anim.beginTime = CACurrentMediaTime() + (i - beginIndex) * 0.1;
        [subview pop_addAnimation:anim forKey:nil];
        
        // 监听最后一个动画
        if (i == self.subviews.count - 1) {
            [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
                [self removeFromSuperview];
            }];
        }
    }
}

@end
